package com.htudata.zabbix.api.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class GroupZabbixApiService {

  @Autowired
  private BaseZabbixApiService baseZabbixApiService;

  public String createGroups(JSONArray groups) throws Exception {
    JSONObject request = new JSONObject();
    request.put("jsonrpc", "2.0");
    request.put("method", "hostgroup.create");
    request.put("params", groups);
    request.put("id", Integer.valueOf(0));
    return this.baseZabbixApiService.getJSONResult(request);
  }
  
  public String updateGroups(JSONArray groups) throws Exception{
    JSONObject request = new JSONObject();
    request.put("jsonrpc", "2.0");
    request.put("method", "hostgroup.update");
    request.put("params", groups);
    request.put("id", Integer.valueOf(0));
    return this.baseZabbixApiService.getJSONResult(request);
  }
  
  public String deleteGroups(JSONArray groupIds)throws Exception {
    JSONObject request = new JSONObject();
    request.put("jsonrpc", "2.0");
    request.put("method", "hostgroup.delete");
    request.put("params", groupIds);
    request.put("id", Integer.valueOf(0));
    return this.baseZabbixApiService.getJSONResult(request);
  }
  
  public String getGroups(JSONObject params) throws Exception{
    JSONObject request = new JSONObject();
    request.put("jsonrpc", "2.0");
    request.put("method", "hostgroup.get");
    request.put("params", params);
    request.put("id", Integer.valueOf(0));
    return this.baseZabbixApiService.getJSONResult(request);
  }
  
  @Deprecated
  private JSONArray getAllGroups(String[] output) throws Exception{
    JSONObject params = new JSONObject();
    params.put("output", output);
    JSONObject request = new JSONObject();
    request.put("jsonrpc", "2.0");
    request.put("method", "hostgroup.get");
    request.put("params", params);
    request.put("id", Integer.valueOf(1));
    return this.baseZabbixApiService.getJSONArrayResult(request);
  }
}
